package com.hshh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hshh.common.Constant;
import com.hshh.dao.CommonCodeDAO;
import com.hshh.dao.CompanyDAO;
import com.hshh.domain.CompanyVO;
import com.hshh.model.CompanyModel;

@Controller
@RequestMapping(value={"/company/"})
public class CompanyController extends CommonController{
	@Autowired CompanyDAO companyDAO;
	@Autowired CommonCodeDAO commonCodeDAO;
	String TILESForder = "company/";
	/**
	 * 리스트
	 * @return
	 */
	@RequestMapping(value={"list"})
	public String list(){
		return TILESForder+"list"+Constant.TILES;
	}

	/**
	 * 상세보기
	 * @return
	 */
	@RequestMapping(value={"view"})
	public String view(){
		return TILESForder+"view"+Constant.TILES;
	}

	/**
	 * 작성 수정
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value={"edit"})
	public String edit(Model model, @RequestParam(required=false) Integer companyId, @ModelAttribute CompanyModel companyModel){
    	HashMap<String, Object> paramMap = new HashMap<String, Object>();
    	CompanyVO company = new CompanyVO();
        if (companyId!=null) {
    		paramMap.put("companyId", companyId);
    		company = companyDAO.selectCompany(paramMap);
    		companyModel.setSubmit("update");
        }else{
			companyModel.setSubmit("insert");
        }

    	companyModel.setCompany(company);

		// 등록 및 수정에서 사용하는 item
		model = formItem(model, paramMap);
		paramMap.put("group", "measure_target");
		List<Map<String, Object>> meansureTargetList = (List<Map<String, Object>>) commonCodeDAO.selectCode(paramMap);
		model.addAttribute("meansureTargetList", meansureTargetList);
		
		
		return TILESForder+"edit"+Constant.TILES;
	}

    /**
     * 등록 및 수정에서 사용하는 item
     * @param model
     * @param paramMap
     * @return
     */
	private Model formItem(Model model, HashMap<String, Object> paramMap) {

		// 측성 대상구분
    	paramMap.put("type", Constant.MEASURE_TARGET);
    	model.addAttribute("group", commonCodeDAO.selectCode(paramMap));

    	return model;
	}
}
